//
//  GBInterMaster_LogicLauncher.m
//  NetworkTest
//
//  Created by xiaosi on 16/7/26.
//  Copyright © 2016年 xiaosi. All rights reserved.
//

#import "GBInterMaster_LogicLauncher.h"


@class GBInterMaster_DeviceLogic13001;
@implementation GBInterMaster_LogicLauncher


    + (void)launching:(NSData*) data withService:(GBInterMaster_Launcher*)service{
        GBInterMaster_NetData* netData = [[GBInterMaster_NetData alloc] init];
        PTCP ptcp = [GBInterMaster_PackUtil unpack10:netData outData:data];
        if (ptcp == PTCP_ERR_INVALID_BEGIN_TAG
            || ptcp == PTCP_ERR_OUT_OF_RANGE
            || ptcp == PTCP_ERR_LESS_THAN_FIXED) {
            NSLog(@"rec:非法数据头");
        } else if (ptcp != PTCP_OK) {
            NSLog(@"rec:非法数据");
        } else {
            int result =((Byte *)[netData.bodyData bytes])[0] & 0xFF;
            if (result == 0x06) {
                NSLog(@"rec:连接未注册，自动重新注册");
                service.registerTag = false;
                [GBInterMaster_APPLogic connRegister:service];
            } else if (result == 0x07 && service.currentWindow == 0x01) {
                NSLog(@"rec:未登录，自动重新登录");
                [GBInterMaster_APPLogic requestLogin:0x6F withUserInfo:service.loginUser withService:service];
            }
            if (netData.cmdType == PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM) {
                NSLog(@"rec:APP系统指令");
                [GBInterMaster_LogicLauncher appLaunching:netData withService:service];
            } else if (netData.cmdType == PTCP_V10_VALUE_CMD_TYPE_APP_DEVICE) {
                NSLog(@"rec:设备设备指令");
                [GBInterMaster_LogicLauncher deviceLaunching:netData withService:service];
            } else if (netData.cmdType == PTCP_V10_VALUE_CMD_TYPE_SERVER_PUSH) {
                NSLog(@"rec:APP推送指令");
                [GBInterMaster_LogicLauncher serverLaunching:netData withService:service];
            } else {
                
            }
        }


    }
    
    + (void)launchingNetworkError:(int)codeNumber withCmdType:(int)cmdType withCmd:(int)cmd  withService:(GBInterMaster_Launcher*)service{
        
    }
    
    + (void)appLaunching:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
        if (netData.cmd == (LOGIC_APP_CMD_CONNREGISTER | 0x8000)) {//连接注册反馈
            NSLog(@"rec:连接注册反馈");
            if([GBInterMaster_APPLogic responseConnRegister:netData withService:service] == 0x00) {
                if (service.loginUser.loginTag) {
                    [GBInterMaster_APPLogic requestLogin:0x6f withUserInfo:service.loginUser withService:service ];
                }
            }
        }else if (netData.cmd == (LOGIC_APP_CMD_USERREGISTERVEGISTER | 0x8000)) {//用户注册反馈(获取校验码)
            NSLog(@"rec:用户注册反馈(获取校验码)");
            [GBInterMaster_APPLogic responseUserRegisterVegister:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_USERREGISTER | 0x8000)) {// 用户注册反馈
            
            [GBInterMaster_APPLogic responseUserRegister:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_RESETPWDVEGISTER | 0x8000)) {// 忘记密码反馈(获取校验码)
            
            [GBInterMaster_APPLogic responseUserForgetPwdVegister:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_RESETPWD | 0x8000)) {// 找回密码反馈
            
            [GBInterMaster_APPLogic responseUserForgetPwd:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_RESETPWDNEW | 0x8000)) {// 找回密码(设置新密码)反馈
            
            [GBInterMaster_APPLogic responseUserNewPwd:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_LOGIN | 0x8000)) {// 用户登录反馈
            NSLog(@"rec:用户登录反馈");
            int result =((Byte *)[netData.bodyData bytes])[0] & 0xFF;
            if (netData.msgId == 0x6F) {
//                if (result != 0x00) {
//                    BroadcastLogic.sendRequestSpecialLoginFail(service);
//                }
            } else {
                if (result == 0x00) {
                    service.loginUser.loginTag = true;
                }
                [GBInterMaster_APPLogic responseLogin:netData withService:service];
                
            }
        } else if (netData.cmd == (LOGIC_APP_CMD_UPDATEPWD | 0x8000)) {// 修改密码反馈
            
            [GBInterMaster_APPLogic responseUpdatePwd:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_ACCEPTPOSSESSOR | 0x8000)) {// 所有者认证反馈
            
            [GBInterMaster_APPLogic responseAcceptPossessor:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_UPDATEDEVICEINFO | 0x8000)) {// 修改设备信息反馈
            
            [GBInterMaster_APPLogic responseDeviceRename:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETDEVICELIS | 0x8000)) {// 获取设备列表反馈
            NSLog(@"rec:获取设备列表反馈");
            [GBInterMaster_APPLogic responseDeviceList:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_DEVICEUSERLIST | 0x8000)) {// 获取使用者列表反馈
            
            [GBInterMaster_APPLogic responseDeviceUserList:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_REMOVEDEVICE | 0x8000)) {
            
            [GBInterMaster_APPLogic responseRelieveDevice:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_ADDDEVICEUSER | 0x8000)) {// 添加使用者反馈
            
            [GBInterMaster_APPLogic responseAddDeviceUser:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_DELETEDEVICEUSER | 0x8000)) {// 删除使用者反馈
            
            [GBInterMaster_APPLogic responseDeleteDeviceUser:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_UPDATEDEVICEUSER | 0x8000)) {// 编辑使用者反馈
            
            [GBInterMaster_APPLogic responseEditDeviceUser:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_DEVICEOWNCHANGE | 0x8000)) {// 所有者转移反馈
            
            [GBInterMaster_APPLogic responseDeviceOwnChange:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETPROGRAMINFO | 0x8000)) {// 获取分时段信息反馈
            
            [GBInterMaster_APPLogic responseShowProgramInfo:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_MSGRECEIVEMODEL | 0x8000)) {// (消息推送设置)数据同步反馈
            
            [GBInterMaster_APPLogic responseMsgReceiveMode:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETDEVICECONFIG | 0x8000)) {// 获取设备系统配置
            
            [GBInterMaster_APPLogic responseDeviceConfig:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETDEVICEPARAM | 0x8000)) {// 获取设备参数
            
            [GBInterMaster_APPLogic responseDeviceParam:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETDEVICESTATUS | 0x8000)) {// 获取设备状态
            
            [GBInterMaster_APPLogic responseDeviceStatus:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_REQUESTUSERFEEDBACK | 0x8000)) {// 用户反馈
           
            [GBInterMaster_APPLogic responseUserFeedBack:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETUSEROPES | 0x8000)) {// 获取用户操作记录
            
            [GBInterMaster_APPLogic responseUserOpes:netData withService:service];
        } else if (netData.cmd == (LOGIC_APP_CMD_GETDEVICERUNRECORD | 0x8000)) {// 获取设备运行记录
            
            [GBInterMaster_APPLogic responseDeviceRuns:netData withService:service];
        }
    }
    
    + (void)deviceLaunching:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
        if (netData.bodyData.length < 17) {
            return;
        }
        Byte* bytes = (Byte*)[netData.bodyData bytes];
        int result = bytes[0];
        NSString* deviceAccount = [[NSString alloc] initWithBytes:bytes+1 length:16 encoding:NSUTF8StringEncoding];
        if (netData.cmd == (LOGIC_CMD_REQUEST_POWERSWITCH | 0x8000)) {// 开关机
            NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":deviceAccount};
             [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_SET_POWERSWITCH object:nil userInfo:userData];
        }if (netData.cmd == (LOGIC_CMD_REQUEST_WORKMODEL | 0x8000)) {// 工作模式
            NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":deviceAccount};
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_SET_WORKMODE object:nil userInfo:userData];
        }if (netData.cmd == (LOGIC_CMD_REQUEST_HEATMODE | 0x8000)) {// 加热模式
            NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":deviceAccount};
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_SET_HEATMODE object:nil userInfo:userData];
        }if (netData.cmd == (LOGIC_CMD_REQUEST_TEMPEHM | 0x8000)) {// 设置温度
            NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":deviceAccount};
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_SET_TEMPE object:nil userInfo:userData];
        }if (netData.cmd == (LOGIC_CMD_REQUEST_DAYPARTING | 0x8000)) {// 设置时段
            NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAccount":deviceAccount};
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_SET_PROGRAM object:nil userInfo:userData];
        }
    }
    
    + (void)serverLaunching:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
        if (netData.cmd == LOGIC_APP_CMD_PUSH_RUNSTATE) {// 设备运行状态推送
            if(netData.length < 18){
                return ;
            }
            Byte* bys = (Byte*)[netData.bodyData bytes];
            int deviceType =(bys[0] & 0x00FF)  + ((bys[1] & 0x00FF) << 8 & 0xFF00);
            if(deviceType == DEVICE_TYPE_AMPUX_GM8_13001){
                [GBInterMaster_DeviceLogic13001 pushRunningState:netData ];
            }
            
        } else if (netData.cmd == LOGIC_APP_CMD_PUSH_ADDDEVICEUSER) {// 添加使用者推送
            
        } else if (netData.cmd == LOGIC_APP_CMD_PUSH_DELETEDEVICEUSER) {// 删除使用者推送
            
        } else if (netData.cmd == LOGIC_APP_CMD_PUSH_DEVICEOWNCHANGE) {// 所有者转移推送
            
        }
    }

@end
